// export default store => next => action => {
//   console.log(store);
//   console.log('dispatch', action);
//   next(action);
//   // store.dispatch(action);
//   console.log('finished', action);
// }

// export default function (store) {
//   return function (_ref) {
//     var dispatch = _ref.dispatch;
//     var getState = _ref.getState;
//     return function (next) {
//       return function (action) {
//         return action(dispatch, getState, store);
//       }
//     }
//   }
// }
// export default (extraArgument) => (_ref) => {
//   const {dispatch, getState} = _ref;
//   return next => action => {
//     if (typeof action === 'function') {
//       return action(dispatch, getState, extraArgument);
//     }
//     return next(action);
//   }
// }
export default (store) => next => action => {
  const {dispatch, getState} = store;
  console.log(action);
  if (typeof action === 'function') {
    return action(dispatch, getState, store);
  }
  return next(action);
}

// function (extraArgument) {
//   console.log(extraArgument)
//   return function (_ref) {
//     var dispatch = _ref.dispatch,
//         getState = _ref.getState;
//     return function (next) {
//       return function (action) {
//         console.log(typeof action);
//         if (typeof action === 'function') {
//           return action(dispatch, getState, extraArgument);
//         }

//         return next(action);
//       };
//     };
//   };
// }

const fetchMiddleware = store => next => action => {
  if (!action.url || !Array.isArray(action.types)) {
    return next(action);
  }
  const [LOADING, SUCCESS, ERROR] = action.types // 数组结构赋值
  next({
    type: LOADING,
    loading: true,
    ...action
  });
  fetch(action.url, {params: action.params}).then(res => {
    next({
      type: SUCCESS,
      loading: false,
      payload: res
    });
  }).catch(err => {
    next({
      type: ERROR,
      loading: false,
      error: err
    });
  });
}